package com.gaogzhen.controller;

import com.gaogzhen.domain.SysMenu;
import com.gaogzhen.service.SysRolePrivilegeService;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.RolePrivilegesParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description 角色权限管理器
 * @date 2022-05-26 10:38
 */
@RestController
@Api(tags = "角色权限配置")
public class SysRolePrivilegeController {

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @GetMapping("/roles_privileges")
    @ApiOperation(value = "查询角色的权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id")
    })
    public R<List<SysMenu>> findMenusAndPrivileges(Long roleId) {

        List<SysMenu> menus = sysRolePrivilegeService.findMenusAndPrivileges(roleId);
        return R.ok(menus);
    }

    @PostMapping("/grant_privileges")
    @ApiOperation(value = "授予角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rolePrivilegesParam", value = "角色权限参数对象")
    })
    public R<List<SysMenu>> grantPrivileges(@RequestBody RolePrivilegesParam rolePrivilegesParam) {

        boolean isOk = sysRolePrivilegeService.grantPrivileges(rolePrivilegesParam);
        return isOk ? R.ok() : R.fail("授予失败");
    }
}
